using Domain.Entities;
using Domain.Misc;
using Domain.Services;
using System;
using UserInterface.Misc;

namespace UserInterface.Admin
{
    public partial class Settings : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Domain.Entities.Settings settings = BoardSettingsManager.Instance.Settings;

                BoardNameTextBox.Text = settings.BoardName;
                AllowNewRegistrationsRadioButtonList.SelectedValue = settings.AllowNewRegistrations.ToString();

                AccountActivationByEmailRadioButtonList.SelectedValue = settings.AccountActivationByEmail.ToString();
                AccountActivationByAdminRadioButtonList.SelectedValue = settings.AccountActivationByAdmin.ToString();

                ThemeDropDownList.DataValueField = "Id";
                ThemeDropDownList.DataTextField = "Name";

                ThemeDropDownList.DataSource = ThemeService.GetAll();
                ThemeDropDownList.DataBind();

                ThemeDropDownList.SelectedValue = settings.DefaultTheme.Id.ToString();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            BoardAdmin masterPage = Master as BoardAdmin;

            if (Page.IsValid)
            {
                string boardName = BoardNameTextBox.Text.Trim();
                bool allowNewRegistrations = Convert.ToBoolean(AllowNewRegistrationsRadioButtonList.SelectedValue);
                bool accountActivationByEmail = Convert.ToBoolean(AccountActivationByEmailRadioButtonList.SelectedValue);
                bool accountActivationByAdmin = Convert.ToBoolean(AccountActivationByAdminRadioButtonList.SelectedValue);

                SettingsService.UpdateGlobalSettings(boardName, allowNewRegistrations, accountActivationByEmail, accountActivationByAdmin);
            }
            else
            {
                masterPage.Messages.AddCritical(Resources.Resource.InvalidFormData);
            }
        }
    }
}
